package com.caigou.service.impl;

import com.caigou.bean.cms.entity.ArticleLaw;
import com.caigou.mapper.cms.ArticleLawMapper;
import com.caigou.service.ArticleLawService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ArticleLawServiceImpl implements ArticleLawService {

    @Resource
    private ArticleLawMapper articleLawMapper;

    @Override
    public ArticleLaw saveArticleLawData(String articleId, Integer lawTimeliness, String lawReferenceSource, String lawReferenceYear, String lawReferenceNumber,
                                         Integer lawReplaceStatus, String lawReplace, Integer lawIssueUnit, Integer lawClass, Integer lawLevel,
                                         String lawIssueTime, String lawImplementTime, String lawOverdueTime, Integer lawEntryLevel, Integer lawEntryStatus,
                                         Integer lawEntryPageStatus, Integer lawPrefixStatus, String lawPrefix) {
        ArticleLaw articleLaw = new ArticleLaw();
        articleLaw.setArticleId(articleId);
        articleLaw.setLawTimeliness(lawTimeliness);
        articleLaw.setLawReferenceSource(lawReferenceSource);
        articleLaw.setLawReferenceYear(lawReferenceYear);
        articleLaw.setLawReferenceNumber(lawReferenceNumber);
        articleLaw.setLawReplaceStatus(lawReplaceStatus);
        articleLaw.setLawReplace(lawReplace);
        articleLaw.setLawIssueUnit(lawIssueUnit);
        articleLaw.setLawClass(lawClass);
        articleLaw.setLawLevel(lawLevel);
        articleLaw.setLawIssueTime(lawIssueTime);
        articleLaw.setLawImplementTime(lawImplementTime);
        articleLaw.setLawOverdueTime(lawOverdueTime.equals("")? null : lawOverdueTime);
        articleLaw.setLawEntryLevel(lawEntryLevel);
        articleLaw.setLawEntryStatus(lawEntryStatus);
        articleLaw.setLawEntryPageStatus(lawEntryPageStatus);
        articleLaw.setLawPrefixStatus(lawPrefixStatus);
        articleLaw.setLawPrefix(lawPrefix);
        articleLawMapper.saveArticleLawData(articleLaw);
        return articleLaw;
    }

    @Override
    public int deleteArticleLawData(String articleId) {
        return articleLawMapper.deleteArticleLawData(articleId);
    }

    @Override
    public int updateArticleLawData(String articleId, Integer lawTimeliness, String lawReferenceSource, String lawReferenceYear, String lawReferenceNumber,
                                    Integer lawReplaceStatus, String lawReplace, Integer lawIssueUnit, Integer lawClass, Integer lawLevel,
                                    String lawIssueTime, String lawImplementTime, String lawOverdueTime, Integer lawEntryLevel, Integer lawEntryStatus,
                                    Integer lawEntryPageStatus, Integer lawPrefixStatus, String lawPrefix) {
        ArticleLaw articleLaw = articleLawMapper.loadArticleLawData(articleId);
        boolean isCreated = false;
        if (null == articleLaw){
            isCreated = true;
            articleLaw = new ArticleLaw();
        }
        articleLaw.setArticleId(articleId);
        articleLaw.setLawTimeliness(lawTimeliness);
        articleLaw.setLawReferenceSource(lawReferenceSource);
        articleLaw.setLawReferenceYear(lawReferenceYear);
        articleLaw.setLawReferenceNumber(lawReferenceNumber);
        articleLaw.setLawReplaceStatus(lawReplaceStatus);
        articleLaw.setLawReplace(lawReplace);
        articleLaw.setLawIssueUnit(lawIssueUnit);
        articleLaw.setLawClass(lawClass);
        articleLaw.setLawLevel(lawLevel);
        articleLaw.setLawIssueTime(lawIssueTime);
        articleLaw.setLawImplementTime(lawImplementTime);
        articleLaw.setLawOverdueTime(lawOverdueTime);
        articleLaw.setLawEntryLevel(lawEntryLevel);
        articleLaw.setLawEntryStatus(lawEntryStatus);
        articleLaw.setLawEntryPageStatus(lawEntryPageStatus);
        articleLaw.setLawPrefixStatus(lawPrefixStatus);
        articleLaw.setLawPrefix(lawPrefix);
        if (isCreated){
            return articleLawMapper.saveArticleLawData(articleLaw);
        } else {
            return articleLawMapper.updateArticleLawData(articleLaw);
        }
    }

    @Override
    public ArticleLaw loadArticleLawData(String articleId) {
        return articleLawMapper.loadArticleLawData(articleId);
    }
}
